home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / POP2CLI.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-07  |  3.7 KB  |  160 lines

  1. /* POP2 Client routines -- Not recommended.
  2.  *    Jan 92    William Allen Simpson
  3.  *        complete re-write to match new mailreader commands
  4.  *
  5.  *    partly based on a NNTP client design by Anders Klemets, SM0RGV
  6.  *    Originally authored by Mike Stockett (WA7DYX).
  7.  *
  8.  * History:
  9.  *    Modified 12 May 1991 by Mark Edwards (WA6SMN) to use new timer
  10.  *    facilities in NOS0423.  Fixed type mismatches spotted by C++.
  11.  *    Modified 27 May 1990 by Allen Gwinn (N5CKP) for compatibility
  12.  *      with later releases (NOS0522).
  13.  *    Added into NOS by PA0GRI (and linted into "standard" C)
  14.  *     Modified 14 June 1987 by P. Karn for symbolic target addresses,
  15.  *      also rebuilt locking mechanism
  16.  *
  17.  *    Some code culled from previous releases of SMTP.
  18.  *    See that code for applicable copyright notices.
  19.  */
  20. #include "global.h"
  21. #ifdef POP2CLIENT
  22. #include "timer.h"
  23. #include "proc.h"
  24.  
  25. #if !defined(_lint)
  26. static char rcsid[] OPTIONAL = "$Id: pop2cli.c,v 1.11 1997/09/07 21:18:28 root Exp root $";
  27. #endif
  28.  
  29. extern char Badhost[];
  30.  
  31. /* Response string keys */
  32. static char *greeting_rsp  = "+ POP2 ";
  33.  
  34. void
  35. pop2_job(unused,v1,p2)
  36. int unused;
  37. void *v1;
  38. void *p2;
  39. {
  40. struct mailservers *np = v1;
  41. struct sockaddr_in fsocket;
  42. char buf[TLINELEN];
  43. char *cp;
  44. FILE *wfp = NULLFILE;
  45. int s = -1;
  46. int folder_len;
  47. int msg_num = 0;
  48.  
  49.     if (mailbusy (np))
  50.         return;
  51.  
  52.     if ((fsocket.sin_addr.s_addr = resolve (np->hostname)) == 0L) {
  53.         /* No IP address found */
  54.         if (Mailtrace >= 1)
  55.             log (-1, "POP2 can't resolve host '%s'", np->hostname);
  56.         start_detached_timer (&np->timer);
  57.         return;
  58.     }
  59.  
  60.     fsocket.sin_family = AF_INET;
  61.     fsocket.sin_port = IPPORT_POP2;
  62.  
  63.     s = socket (AF_INET, SOCK_STREAM, 0);
  64.     sockmode (s, SOCK_ASCII);
  65.  
  66.     if (connect (s, (char *)&fsocket, SOCKSIZE) == -1) {
  67.         cp = sockerr (s);
  68.         if (Mailtrace >= 2)
  69.             log (s, "POP2 Connect failed: %s", (cp != NULLCHAR) ? cp : "");
  70.         goto quit;
  71.     }
  72.  
  73.     log (s, "POP2 Connected to mailhost %s", np->hostname);
  74.  
  75.     if (mailresponse (s, buf, "banner") == -1)
  76.         goto quit;
  77.  
  78.     if (strncmp (buf, greeting_rsp, strlen (greeting_rsp)) != 0)
  79.         goto quitcmd;
  80.  
  81.     (void)usprintf (s, "HELO %s %s\n", np->username, np->password);
  82.  
  83.     if (mailresponse (s, buf, "HELO") == -1)
  84.         goto quit;
  85.  
  86.     if (buf[0] != '#' || (folder_len = atoi (&(buf[1]))) == 0) {
  87.         /* If there is no mail (the only time we get a "+"
  88.          * response back at this stage of the game),
  89.          * then just close out the connection, because
  90.          * there is nothing more to do!! */
  91.         goto quitcmd;
  92.     }
  93.  
  94.     if ((wfp = tmpfile()) == NULLFILE) {
  95.         if ( Mailtrace >= 1 )
  96.             log (s, "POP2 Cannot create tmp file");
  97.         goto quitcmd;
  98.     }
  99.  
  100.     (void)usputs (s, "READ\n");
  101.  
  102.     /* now, get the text */
  103.     while(TRUE) {
  104.         long msg_len;
  105.  
  106.         if (mailresponse (s, buf, "read loop") == -1)
  107.             goto quit;
  108.  
  109.         if (buf[0] == '=' && (msg_len = atol (&(buf[1]))) > 0) {
  110.             (void)usputs (s, "RETR\n");
  111.  
  112.             while (msg_len > 0) {
  113.                 if (recvline (s, buf, TLINELEN) == -1)
  114.                     goto quit;
  115.  
  116.                 rip (buf);
  117.                 if(!strncmp (buf, "From ", 5))
  118.                     fputc ('>', wfp);
  119.  
  120.                 fprintf (wfp, "%s\n", buf);
  121.  
  122.                 msg_len -= (long)(strlen (buf)+2);/* Add CRLF */
  123.             }
  124.             (void)usputs (s, "ACKD\n");
  125.  
  126.             msg_num++;
  127.         } else {
  128.             break;
  129.         }
  130.     }
  131.  
  132.     if (folder_len > 0) {
  133.         /* testing the result is pointless,
  134.          * since POP2 already deleted mail.
  135.          */
  136.         copymail (buf, TLINELEN, wfp, np->mailbox);
  137.  
  138.         if (Mailtrace) {
  139.             if (Mailquiet == 0)
  140.                 tcmdprintf ("\007");
  141.             tcmdprintf ("New mail arrived for %s from mailhost %s\n",
  142.                 np->mailbox, np->hostname);
  143.             smtptick (NULL);     /* wake SMTP to send that mail */
  144.         }
  145.     }
  146.  
  147. quitcmd:
  148.     (void)usputs (s, "QUIT\n");
  149.  
  150. quit:
  151.     log (s, "POP2 daemon exiting");
  152.     close_s (s);
  153.  
  154.     if (wfp != NULLFILE)
  155.         fclose (wfp);
  156.     start_detached_timer (&np->timer);
  157. }
  158.  
  159. #endif
  160.